package com.qyx.pmpucat.controller;

import com.qyx.pmpucat.common.result.Result;
import com.qyx.pmpucat.common.result.Results;
import com.qyx.pmpucat.entity.OptionCategory;
import com.qyx.pmpucat.entity.Question;
import com.qyx.pmpucat.mapper.OptionCategoryMapper;
import com.qyx.pmpucat.mapper.QuestionMapper;
import com.qyx.pmpucat.util.ImportTable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据导入控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/data-import")
@RequiredArgsConstructor
public class DataImportController {

    private final ImportTable importTable;
    private final QuestionMapper questionMapper;
    private final OptionCategoryMapper optionCategoryMapper;

    /**
     * 执行数据转化
     * 将AQuestion和AQuestionNum表的数据导入到Question表中
     */
    @PostMapping("/import-questions")
    public Result<String> importQuestions() {
        try {
            log.info("开始执行数据转化...");
            importTable.importData();
            return Results.success("数据转化完成");
        } catch (Exception e) {
            log.error("数据转化失败: {}", e.getMessage(), e);
            return new Result<String>()
                    .setCode("IMPORT_ERROR")
                    .setMessage("数据转化失败: " + e.getMessage());
        }
    }

    /**
     * 清空Question表数据
     * 谨慎使用此接口
     */
    @PostMapping("/clear-questions")
    public Result<String> clearQuestions() {
        try {
            log.warn("开始清空Question表数据...");
            importTable.clearQuestionData();
            return Results.success("Question表数据已清空");
        } catch (Exception e) {
            log.error("清空数据失败: {}", e.getMessage(), e);
            return new Result<String>()
                    .setCode("CLEAR_ERROR")
                    .setMessage("清空数据失败: " + e.getMessage());
        }
    }

    /**
     * 测试获取问题和选项内容
     */
    @GetMapping("/test-question-options")
    public Result<Map<String, Object>> testQuestionOptions() {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 获取前5个问题
            List<Question> questions = questionMapper.selectListByQuery(
                com.mybatisflex.core.query.QueryWrapper.create().limit(5)
            );
            
            for (Question question : questions) {
                Map<String, Object> questionInfo = new HashMap<>();
                questionInfo.put("id", question.getId());
                questionInfo.put("title", question.getTitle());
                questionInfo.put("alphj", question.getAlphj());
                
                if (question.getOptions() != null) {
                    for (Question.QuestionOption option : question.getOptions()) {
                        Map<String, Object> optionInfo = new HashMap<>();
                        optionInfo.put("id", option.getId());
                        optionInfo.put("optionCategoryId", question.getOptionCategoryId());
                        optionInfo.put("betaj", option.getBetaj());
                        optionInfo.put("sort", option.getSort());
                        
                        // 获取选项内容
                        if (question.getOptionCategoryId() != null) {
                            OptionCategory category = optionCategoryMapper.selectOneById(question.getOptionCategoryId());
                            if (category != null && category.getOptions() != null && !category.getOptions().isEmpty()) {
                                optionInfo.put("content", category.getOptions().get(0).getLabel());
                            }
                        }
                        
                        questionInfo.put("option_" + option.getSort(), optionInfo);
                    }
                }
                
                result.put("question_" + question.getId(), questionInfo);
            }
            
            return Results.success(result);
        } catch (Exception e) {
            log.error("测试失败: {}", e.getMessage(), e);
            return new Result<Map<String, Object>>()
                    .setCode("TEST_ERROR")
                    .setMessage("测试失败: " + e.getMessage());
        }
    }
} 